package com.dataaccess.dataaccess.support.utils;

import cn.hutool.core.date.DateUtil;

import java.math.BigInteger;

/**
 * @Author: YangYu
 * @ClassName: HexUtils
 * @Date: Created in 15:40 2023/9/21
 * @Description: 十六进制工具类
 */
public class HexUtils {

    /**
     * @Author YangYu
     * @Description 返回的校验和
     * @Date Created in 9:43 2021/5/19
     * @Param [code]
     */

    public static String checkSum(String code){
        int num = 0;
        for (int j = 0; j <code.length() ; j = j+2) {

            num = num+hex16To10(code.substring(j,j+2));
        }
        String nums = getHex16(num);
        nums = nums.substring(nums.length() -2,nums.length());
        return nums;
    }


    /**
     * @Author YangYu
     * @Description 十六进制转（整型）
     * @Date Created in 9:34 2021/5/19
     * @Param [strHex]
     */

    public static int hex16To10(String strHex) {
        BigInteger lngNum = new BigInteger(strHex, 16);
        return lngNum.intValue();
    }


    /**
     * @Author YangYu
     * @Description int类型转16进制
     * @Date Created in 15:51 2021/4/22
     * @Param [number]
     */

    public static String getHex16(int number) {
        int i = 0;
        char[] S = new char[100];
        String str = "";
        if (number == 0) {
            str = "0";
        } else {
            while (number != 0) {
                int t = number % 16;
                if (t >= 0 && t < 10) {
                    S[i] = (char) (t + '0');
                    i++;
                } else {
                    S[i] = (char) (t + 'A' - 10);
                    i++;
                }
                number = number / 16;
            }

            for (int j = i - 1; j >= 0; j--) {
                //System.out.print(S[j]);
                str = str + S[j];
            }
        }
        return str;
    }
    /**
     * @Author YangYu
     * @Description 十进制字符串转十六进制字符串
     * @Date Created in 10:48 2021/12/15
     * @Param [value]
     */

    public static String hex10To16s(String value) {
        String hex = Integer.toHexString(Integer.parseInt(value));
        if (hex.length() == 1) {
            hex = "0" + hex;
        }
        return hex;
    }

    /**
     * @Author YangYu
     * @Description 十六进制时间（低位在前）
     * @Date Created in 10:48 2021/12/15
     * @Param [value]
     */

    public static String getHexTime() {
        String time = DateUtil.now();
        return hex10To16s(time.substring(17,19))+hex10To16s(time.substring(14,16))+hex10To16s(time.substring(11,13))+hex10To16s(time.substring(8,10))+hex10To16s(time.substring(5,7))+hex10To16s(time.substring(2,4));
    }
    /**
     * @Author YangYu
     * @Description 十六进制转二进制字符串
     * @Date Created in 14:07 2021/11/24
     * @Param [hex]
     */

    public static String hexToBinaryString(String hex) {
        String str = Integer.toBinaryString(Integer.parseInt(hex,16));
        if(str.length() == 1){
            str = "0000000" + str;
        }else if(str.length() == 2){
            str = "000000" + str;
        }else if(str.length() == 3){
            str = "00000" + str;
        }else if(str.length() == 4){
            str = "0000" + str;
        }else if(str.length() == 5){
            str = "000" + str;
        }else if(str.length() == 6){
            str = "00" + str;
        }else if(str.length() == 7){
            str = "0" + str;
        }
        return str;
    }
    /**
     * @Author YangYu
     * @Description 十六进制转二进制字符串
     * @Date Created in 14:07 2021/11/24
     * @Param [hex]
     */

    public static String hexToBinaryStringN(String hex) {
        String str = Integer.toBinaryString(Integer.parseInt(hex,16));
        if(str.length() == 1){
            str = "000000000000000" + str;
        }else if(str.length() == 2){
            str = "00000000000000" + str;
        }else if(str.length() == 3){
            str = "0000000000000" + str;
        }else if(str.length() == 4){
            str = "000000000000" + str;
        }else if(str.length() == 5){
            str = "00000000000" + str;
        }else if(str.length() == 6){
            str = "0000000000" + str;
        }else if(str.length() == 7){
            str = "000000000" + str;
        }else if(str.length() == 8){
            str = "00000000" + str;
        }else if(str.length() == 9){
            str = "0000000" + str;
        }else if(str.length() == 10){
            str = "000000" + str;
        }else if(str.length() == 11){
            str = "00000" + str;
        }else if(str.length() == 12){
            str = "0000" + str;
        }else if(str.length() == 13){
            str = "000" + str;
        }else if(str.length() == 14){
            str = "00" + str;
        }else if(str.length() == 15){
            str = "0" + str;
        }
        return str;
    }
    /**
     * @Author YangYu
     * @Description 十六进制转十进制
     * @Date Created in 16:37 2022/1/12
     * @Param [hex]
     */

    public static int hexToInt(String hex){
        if (hex.length() < 1){
            return 0;
        }else {
            return Integer.parseInt(hex,16);
        }
    }
    /**
     * @Author YangYu
     * @Description 十六进制转负数（大端）
     * @Date Created in 下午4:58 2025/3/27
     * @Param [hex]
     */

    public static int hexToNegative(String hex){
        long value = Long.parseLong(hex, 16);
        byte[] src = new byte[4];
        src[0] = (byte) ((value >> 24) & 0xFF);
        src[1] = (byte) ((value >> 16) & 0xFF);
        src[2] = (byte) ((value >> 8) & 0xFF);
        src[3] = (byte) (value & 0xFF);
        return ((src[3] & 0xFF) << 24) | ((src[2] & 0xFF) << 16) | ((src[1] & 0xFF) << 8) | (src[0] & 0xFF);
    }
    /**
     * @Author YangYu
     * @Description 返回设备的当前时间
     * @Date Created in 13:36 2022/1/10
     * @Param []
     */

    public static String currentTime() {
        String[] waterTime = DateUtil.now().split(" ");
        String date = waterTime[0];
        String[] dateCode = date.split("-");
        String ms = waterTime[1];
        String[] msCode = ms.split(":");
        String currentTime = hex10To16s(msCode[2]) + hex10To16s(msCode[1]) + hex10To16s(msCode[0]) + hex10To16s(dateCode[2]) + hex10To16s(dateCode[1]) + hex10To16s(dateCode[0].substring(2));
        return currentTime;
    }
}
